<%- include('css') %>
<link rel="stylesheet" href="/public/js_module/simditor/css/simditor.css">
<div id="app" style="width:980px">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>案件结案</legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="form">

        <div class="layui-form-item">
            <label class="layui-form-label" v-if="event.event_belong == 1">结案人所属条线部门</label>
            <label class="layui-form-label" v-if="event.event_belong == 2">结案人所属物业公司</label>
            <div class="layui-input-block">
                <select name="top_id" id="top_id" lay-verify="required" lay-search="" lay-filter="finduser">
                    <option value="" >直接选择或搜索选择</option>
                    <!--<option :value="item.id" v-for="(item,index) in streets" :key="index">{{item.name}}</option>-->
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">结案人姓名</label>
            <div class="layui-input-block">
                <select name="user_id" id="user_id" lay-verify="required" lay-search="" lay-filter="user">
                    <option value="" >直接选择或搜索选择</option>
                    <!--<option :value="item.id" v-for="(item,index) in streets" :key="index">{{item.name}}</option>-->
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">结案信息</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" lay-verify="required" name="desc" id="desc"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">结案图片</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="headpic" v-if="pics.length < 3">上传图片</button>
                    <div class="layui-upload-list">
                        <img v-if="pics.length > 0" v-for="item in pics" style="width: 92px; height: 92px; margin: 0 10px 10px 0;" id="headpreview"
                             v-bind:src="item">

                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" v-bind:disabled="isButtonDisabled" lay-filter="save">确定</button>
                <button type="button" v-on:click="doclose" class="layui-btn layui-btn-primary">关闭</button>
            </div>
        </div>
    </form>
</div>

<%- include('script') %>
<!--<script src="/public/js_module/jquery.js"></script>-->
<!--simditor富文本依赖-->
<script src="/public/js_module/simditor/module.js"></script>
<script src="/public/js_module/simditor/hotkeys.js"></script>
<script src="/public/js_module/simditor/uploader.js"></script>
<script src="/public/js_module/simditor/simditor.js"></script>

<script>


    var vm = new Vue({
        el: '#app',
        data:function(){
            return {
                isButtonDisabled:false,
                id:'',
                event:{},
                pics:[]
            }

        },
        mounted:function(){
            this.id = '<%=id%>'
            var self = this;
           // this.getroads();
            layui.use(['form','layer','upload'], function () {
                var form = layui.form;
                var layer = layui.layer;

                var upload = layui.upload;

                //执行实例
                var uploadInst = upload.render({
                    elem: '#headpic' //绑定元素
                    ,
                    url: '/common/upload/zipimg?archive=archive' //上传接口
                    ,
                    multiple: true
                    ,
                    number: 3
                    ,
                    before: function (obj) {
                        //预读本地文件示例，不支持ie8
                        obj.preview(function (index, file, result) {
                            // self.haspic = true
                            // self.pic = result
                        });
                    },
                    done: function (res) {
                        //如果上传失败
                        if (res.code > 0) {
                            // self.pic = '';
                            return layer.msg('上传失败');
                        } else {
                            if(self.pics.length < 3){
                                self.pics.push(res.url);
                            }
                        }
                        //上传成功
                    },
                    error: function () {
                    }
                });


                $.get('/admin/event/getevent',{id:self.id},(res)=>{
                    if(res.code == 200){
                        self.event = res.data;
                        form.render();
                        self.getTop(form);
                    }
                });
                //监听提交
                form.on('submit(save)', function (data) {
                    self.isButtonDisabled = true;
                    data.field.event_id=self.event.id;
                    data.field.pics = self.pics;
                    $.post(
                        '/admin/event/handle',
                        {
                            data:data.field
                        },
                        function(rs){
                            if (rs.code === '200') {
                                parent.vm.iframesave()
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭  
                                parent.layer.msg('结案已完成!')
                                    
                            } else {
                                parent.layer.msg(rs.msg);
                            }
                            self.isButtonDisabled = false;
                        }
                    )
                    return false;
                });
                form.on('select(finduser)',function (data) {
                    console.log(data.value);
                    $('#user_id').html('<option value="">直接选择或搜索选择</option>');
                    var index = layer.load();
                    self.getuser(form,data.value,index);
                });
            });

        },
        
        methods:{

            doclose:function(){
                parent.vm.iframeclose();
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引         
                parent.layer.close(index); //再执行关闭 
            },
            getTop:function(form){
                $.get('/admin/event/gettop',{event:JSON.stringify(this.event)},(res)=>{
                    if(res.code == 200){
                        for (var i = 0; i < res.data.length; i++) {
                            $('#top_id').append(`<option value="${ res.data[i].id}">${ res.data[i].name}</option>`);
                            form.render('select');
                        }
                    }
                });
            },
            getuser:function (form,id,index) {
                $.get('/admin/event/getuser',{id:id,event_belong:this.event.event_belong},function (res) {
                        $('#user_id').empty();
                        if(res.code == '201'){
                            $('#user_id').append(`<option value="">请先选择上一项</option>`);
                        }else if(res.code == '200'){
                            var user = res.data;
                            if(user.length>0){
                                $('#user_id').append(`<option value="">请选择结案人</option>`);
                                for (var i = 0; i < user.length; i++) {
                                    $('#user_id').append(`<option value="${user[i].id}">${user[i].name}</option>`);
                                }
                            }else{
                                $('#user_id').append(`<option value="">无相关用户</option>`);
                            }
                        }
                        form.render('select');
                        layer.close(index)
                    }
                )
            }
        }
    })



    
</script>